package com.example.main

import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.common.view.view.BaseMvvmActivity
import com.example.home.HomeFragment
import com.example.main.databinding.ActivityMainBinding
import com.example.message.MessageFragment
import com.example.mine.MineFragment
import com.example.notice.NoticeFragment
import com.example.service.ServiceFragment

@Route(path = "/module_main/MainActivity")
class MainActivity : BaseMvvmActivity<ActivityMainBinding>() {

    override fun getLayoutId(): Int = R.layout.activity_main

    private val titles = arrayOf("首页", "放映厅", "add", "直播", "我的")

    // 底部导航图标和文本缓存
    private lateinit var homeIcon: ImageView
    private lateinit var cinemaIcon: ImageView
    private lateinit var liveIcon: ImageView
    private lateinit var mineIcon: ImageView
    private lateinit var homeText: TextView
    private lateinit var cinemaText: TextView
    private lateinit var liveText: TextView
    private lateinit var mineText: TextView

    override fun initView() {
        super.initView()
        // 初始化底部导航图标和文本
        homeIcon = mDataBinding.root.findViewById(R.id.home_layout)
        cinemaIcon = mDataBinding.root.findViewById(R.id.cinema_layout)
        liveIcon = mDataBinding.root.findViewById(R.id.live_layout)
        mineIcon = mDataBinding.root.findViewById(R.id.mine_layout)
        homeText = mDataBinding.root.findViewById(R.id.tv_home)
        cinemaText = mDataBinding.root.findViewById(R.id.tv_home)
        liveText = mDataBinding.root.findViewById(R.id.tv_live)
        mineText = mDataBinding.root.findViewById(R.id.tv_mine)


        // 设置ViewPager2适配器
        mDataBinding.vp.adapter = MainFragmentPagerAdapter(this)

        // 设置底部导航点击监听
        mDataBinding.homeLl.setOnClickListener {mDataBinding.vp.currentItem = 0}
        mDataBinding.cinemaLl.setOnClickListener { mDataBinding.vp.currentItem = 1}
        mDataBinding.addLayout.setOnClickListener {mDataBinding.vp.currentItem = 2}
        mDataBinding.liveLl.setOnClickListener {mDataBinding.vp.currentItem = 3}
        mDataBinding.mineLl.setOnClickListener { mDataBinding.vp.currentItem = 4}

        // 设置ViewPager页面变化监听器
        mDataBinding.vp.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                updateBottomNavigation(position)
            }
        })
    }

    private fun updateBottomNavigation(selectedPosition: Int) {
        resetAllIcons()
        when (selectedPosition) {
            0 -> {
                homeIcon.setImageResource(R.drawable.home_selected)
                homeText.setTextColor(ContextCompat.getColor(this, android.R.color.holo_red_light))
            }
            1 -> {
                cinemaIcon.setImageResource(R.drawable.ic_bofang_red)
                cinemaText.setTextColor(ContextCompat.getColor(this, android.R.color.holo_red_light))
            }
            2 -> {
                // 中间 add 按钮处理
            }
            3 -> {
                liveIcon.setImageResource(R.drawable.ic_vidicon_red)
                liveText.setTextColor(ContextCompat.getColor(this, android.R.color.holo_red_light))
            }
            4 -> {
                mineIcon.setImageResource(R.drawable.mine_yes)
                mineText.setTextColor(ContextCompat.getColor(this, android.R.color.holo_red_light))
            }
        }
    }

    private fun resetAllIcons() {
        homeIcon.setImageResource(R.drawable.home)
        cinemaIcon.setImageResource(R.drawable.ic_bofang)
        liveIcon.setImageResource(R.drawable.ic_vidicon)
        mineIcon.setImageResource(R.drawable.mine_no)

        val defaultColor = ContextCompat.getColor(this, android.R.color.darker_gray)
        homeText.setTextColor(defaultColor)
        cinemaText.setTextColor(defaultColor)
        liveText.setTextColor(defaultColor)
        mineText.setTextColor(defaultColor)
    }

    // ViewPager适配器
    inner class MainFragmentPagerAdapter(fragmentActivity: FragmentActivity) :
        FragmentStateAdapter(fragmentActivity) {
        override fun getItemCount(): Int = titles.size

        override fun createFragment(position: Int): Fragment {
            return when (position) {
                0 -> HomeFragment() // 首页Fragment
                1 -> ServiceFragment() // 放映厅Fragment
                2 -> MessageFragment() // add Fragment
                3 -> NoticeFragment() // 直播Fragment
                4 -> MineFragment() // 我的Fragment
                else -> HomeFragment()
            }
        }
    }
}
